package com.sunday.commlibrary;


        import com.google.gson.stream.JsonReader;
        import com.lzy.okgo.callback.AbsCallback;

        import java.io.Serializable;
        import java.lang.reflect.ParameterizedType;
        import java.lang.reflect.Type;

        import okhttp3.Call;
        import okhttp3.Response;

/**
 * Created by liu on 2017/3/15.
 */

public abstract class JsonCallback<T> extends AbsCallback<T> {

    @Override
    public T convertSuccess(Response response) throws Exception {

        //com.lzy.demo.callback.DialogCallback<com.lzy.demo.model.LzyResponse<com.lzy.demo.model.ServerModel>> 得到类的泛型，包括了泛型参数
        Type genType = getClass().getGenericSuperclass();
        //从上述的类中取出真实的泛型参数，有些类可能有多个泛型，所以是数值
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
        //我们的示例代码中，只有一个泛型，所以取出第一个，得到如下结果
        //com.lzy.demo.model.Login
        Type type = params[0];
        JsonReader jsonReader = new JsonReader(response.body().charStream());

        //有数据类型，表示有data
        ResultDO<T> lzyResponse = Convert.fromJson(jsonReader, type);
        response.close();

        return (T) lzyResponse;
    }

    @Override
    public void onSuccess(T t, Call call, Response response) {
        ResultDO<T> resultDO = (ResultDO<T>) t;
        if(resultDO.getCode() != 0){

            return;
        }
        onSuccess(t);
    }

    public abstract void onSuccess(T t);
}
